package com.longnows.appserver.api.v1;

import com.longnows.appserver.bo.PageCounter;
import com.longnows.appserver.dto.PersonDTO;
import com.longnows.appserver.exception.http.NotFoundException;
import com.longnows.appserver.model.Spu;
import com.longnows.appserver.service.SpuService;
import com.longnows.appserver.utils.CommonUtil;
import com.longnows.appserver.vo.PagingDozer;
import com.longnows.appserver.vo.SpuSimplifyVo;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.Map;

@RestController
@RequestMapping("/v1/spu")
@Validated
public class SpuController {

    @Autowired
    private SpuService spuService;

    @GetMapping("/id/{id}/detail")
    public Spu getDetail(@PathVariable @Positive Long id) {
        Spu spu = this.spuService.findOneById(id);
       if (spu == null){
           throw  new NotFoundException(30003);
       }
        return spu;
    }

    @GetMapping("/id/{id}/simplify")
    public SpuSimplifyVo getSimplify(@PathVariable @Positive(message = "{id.positive}") Long id) {
        Spu spu = this.spuService.findOneById(id);
        SpuSimplifyVo vo = new SpuSimplifyVo();
        BeanUtils.copyProperties(spu,vo);
        return vo;
    }


    @GetMapping("/latest")
    public PagingDozer<Spu,SpuSimplifyVo> getLatestSpuList(
            @RequestParam(defaultValue = "0") Integer start,
            @RequestParam(defaultValue = "5") Integer count
                                                ){
        PageCounter pageCounter = CommonUtil.converToPageParameter(start,count);
        Page<Spu> pageSpu= this.spuService.getLatestPagingSpu(pageCounter.getPage(),pageCounter.getCount());
        return  new PagingDozer<Spu,SpuSimplifyVo>(pageSpu,SpuSimplifyVo.class);
    }

    @GetMapping("/by/category/{id}")
    public PagingDozer<Spu,SpuSimplifyVo> getByCategoryId(
            @PathVariable @Positive Long id,
            @RequestParam(name="is_root",defaultValue = "false") Boolean isRoot,
            @RequestParam(defaultValue = "0") Integer start,
            @RequestParam(defaultValue = "10") Integer count
    ){
        PageCounter pageCounter = CommonUtil.converToPageParameter(start,count);
        Page<Spu> pageSpu = this.spuService.getByCategory(id,isRoot,pageCounter.getPage(),pageCounter.getCount());
        return  new PagingDozer<Spu,SpuSimplifyVo>(pageSpu,SpuSimplifyVo.class);
    }

    @RestController
    @RequestMapping("/v1")
    @Validated
    public static class TestController {

        @GetMapping("/test/{id}")
        public String getTest(@PathVariable Integer id, @RequestParam String name) {
    //        throw new RuntimeException("啊啊啊");
            throw new NotFoundException(10000);
        }

        @PostMapping("/test2/{id}")
        public String getTest2(@RequestBody Map<String,Object> person, @PathVariable Integer id) {

            throw new NotFoundException(10000);
        }

        @PostMapping("/test3/{id}")
        public String getTest3(@RequestBody @Validated PersonDTO person, @PathVariable  @Range(min=0, max = 10,message = "错误不可以超过10哦") Integer id) {
    //

            PersonDTO personDTO=  PersonDTO.builder().name("苏志冬").age(30).build();

            throw new NotFoundException(10000);
        }

        @PostMapping("/test4/{id}")
        public String getTest4(@RequestBody  Map<String,Object> person, @PathVariable @Range(min=0, max = 10,message = "错误不可以超过10哦") Integer id) {

            throw new NotFoundException(10000);
        }

    }
}
